<?php

/*
 *	Framework namespace
 */
namespace Nano
{
	/*
	 *	Protection class
	 */
	final class Protect
	{
		/*
		 *	Generate hash
		 *
		 *	@param	string	Data to hash
		 *	@param	string	Salt
		 *
		 *	@return	string	Hashed string
		 */
		static public function hash($string, $salt = null)
		{
			return hash('sha512', md5($string), (isset($salt) ? $salt : strlen($string)));
		}
		
		/*
		 *	Generate random hash
		 *
		 *	@param	string	Data to hash
		 *	@param	string	Salt
		 *
		 *	@return	string	Hashed string with random salt
		 */
		static public function rhash($string, $salt = null)
		{
			return hash('sha512', md5($string), (isset($salt) ? $salt : self::salt()));
		}
		
		/*
		 *	Generate ramdon salt
		 *	http://habrahabr.ru/blogs/php/13726
		 *
		 *	@return	string	Random string
		 *
		 *	@static
		 */
		static public function salt()
		{
			$chars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPRQSTUVWXYZ0123456789'; 
			$code = null; 
			
			$clen = strlen($chars) - 1;
			   
    		while(strlen($code) < $length)
    		{ 
				$code .= $chars[mt_rand(0, $clen)];   
			}
			
			return $code; 
		}
	}
}